import sim.engine.SimState;
import sim.engine.Steppable;
import sim.util.Bag;


public class JailTermReducer implements Steppable {
	private static final long serialVersionUID = 1L;

	public void step(SimState state) {
		EpsteinGrid epState = (EpsteinGrid)state;
		
		Bag agents = epState.grid.allObjects;
		
		Agent agent;
		for(int i = 0; i < agents.size(); i++){
			agent = (Agent)agents.get(i);
			if(agent instanceof Civilian && agent.jailTerm > 0){
				agent.jailTerm--;
				if(agent.jailTerm == 0){
					epState.jailCount--;
					epState.quietCount++;
					
					Bag space = epState.grid.getObjectsAtLocation(agent.loc);
					int num = 0;
					for(Object o : space){
						Agent a = (Agent)o;
						
						if(a instanceof Civilian && a.jailTerm == 0){
							num++;
						}
					}
					
					if(num > 1){
						agent.move(epState);
					}
				}
			}
		}
	}
}
